/*
 * NewsDAO.java
 *
 * Created on 28 marzec 2007, 15:18
 *
 */

package arocms.business;

import arocms.util.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;

/**
 *
 * @author arek
 */
public class NewsDAO implements DaoMethods<News>{
    
    private Logger log = Logger.getLogger(this.getClass());
    
    /** Creates a new instance of NewsDAO */
    public NewsDAO() {
    }
    
    public News createOrUpdate(News obj) {
        if (obj.getId()!=null) return update(obj);
        return create(obj);
    }
    
    public News create(News obj) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.save(obj);
        ses.getTransaction().commit();
        return obj;
    }
    
    public News update(News obj) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.update(obj);
        ses.getTransaction().commit();
        return obj;
    }
    
    public News find(Long pk) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        News news = (News) ses.load(News.class,pk);
        ses.getTransaction().commit();
        return news;
    }
    
    public List<News> findAll() {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        List<News> newsList = (List<News>) ses.createQuery("from News").list();
        ses.getTransaction().commit();
        return newsList;
    }
    
    public boolean delete(Long pk) {
        return delete(find(pk));
    }
    
    public boolean delete(News obj) {
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.delete(obj);
        ses.getTransaction().commit();
        return true;
    }
       
}
